/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.engine.entity;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.engine.entity.propertyinfobuilder.BefPropInfoGenerator;
import com.inspur.edp.bef.engine.entity.propertyinfobuilder.BefPropertyInfoBuilderFactory;
import com.inspur.edp.bef.spi.entity.CodeRuleInfo;
import com.inspur.edp.bef.spi.entity.builtinimpls.BefEntityResInfoImpl;
import com.inspur.edp.bef.spi.entity.columnconfig.ColumnGenerateInfo;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.spi.entity.info.UniqueConstraintInfo;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefModelResInfoImpl;
import com.inspur.edp.das.commonmodel.IGspCommonElement;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.das.commonmodel.entity.element.GspBillCodeGenerateOccasion;
import com.inspur.edp.das.commonmodel.entity.object.GspUniqueConstraint;
import java.util.ArrayList;

public class EntityResourceInfo extends BefEntityResInfoImpl {
    private final GspBizEntityObject obj;
    private static BefPropertyInfoBuilderFactory propInfoBuilderFactory=new BefPropertyInfoBuilderFactory();
    private BeModelResInfo beModelResInfo;
    private GspBusinessEntity businessEntity;

    public EntityResourceInfo(GspBizEntityObject obj,BeModelResInfo beModelResInfo,
                              GspBusinessEntity businessEntity) {
        super(obj.getCode(),obj.getI18nResourceInfoPrefix()+".Name",beModelResInfo,obj.getID());
        this.beModelResInfo = beModelResInfo;
        this.businessEntity = businessEntity;
        this.obj = obj;
        initColumns1();
        initUQConstraintInfos1();
        initChildInfos1(beModelResInfo);
        if(isRoot()&&businessEntity.getVersionContronInfo()!=null&&businessEntity.getVersionContronInfo().getVersionControlElementId()!=null&&businessEntity.getVersionContronInfo().getVersionControlElementId()!="")
            setVersionControlPropertyName(obj.findElement(businessEntity.getVersionContronInfo().getVersionControlElementId()).getLabelID());
        intCodeRuleInfos();
        initColumnGenerateInfo();
    }

    private void intCodeRuleInfos() {
        for (IGspCommonField item : obj.getContainElements()) {
            IGspCommonElement element = (IGspCommonElement) item;

            if(!element.getBillCodeConfig().getCanBillCode())
                continue;

            if (element.getBillCodeConfig().getCodeGenerateOccasion() == GspBillCodeGenerateOccasion.SavingTime) {
                addBeforeSaveCodeRuleInfo(element);
            }
            else if (element.getBillCodeConfig().getCodeGenerateOccasion() == GspBillCodeGenerateOccasion.CreatingTime){
                addAfterCreateCodeRuleInfo(element);
            }
        }
    }
    private void initColumnGenerateInfo() {
        ColumnGenerateInfo info=new ColumnGenerateInfo();
        info.setGenerateType(obj.getColumnGenerateID().getGenerateType());
        if(obj.getColumnGenerateID().getCloumnParameter()!=null &&obj.getColumnGenerateID().getCloumnParameter().size()>0) {
            info.getParams().addAll(obj.getColumnGenerateID().getCloumnParameter());
        }
        this.setColumnGenerateInfo(info);
    }
    private void addBeforeSaveCodeRuleInfo(IGspCommonElement element) {
        CodeRuleInfo codeRuleInfo=new CodeRuleInfo();
        codeRuleInfo.setCodeRuleId(element.getBillCodeConfig().getBillCodeID());
        getBeforeSaveCodeRules().put(element.getLabelID(),codeRuleInfo);
    }

    private void addAfterCreateCodeRuleInfo(IGspCommonElement element) {
        CodeRuleInfo codeRuleInfo=new CodeRuleInfo();
        codeRuleInfo.setCodeRuleId(element.getBillCodeConfig().getBillCodeID());
        getAfterCreateCodeRules().put(element.getLabelID(),codeRuleInfo);
    }

    @Override
    protected void initColumns() {
        super.initColumns();

    }

    protected void initColumns1() {

        for (IGspCommonField field : obj.getContainElements()) {
            if(field.getObjectType() == GspElementObjectType.Association && (field.getChildAssociations() ==null
                    || field.getChildAssociations().size() == 0)){
                continue;
            }
            addPropertyInfo(new BefPropInfoGenerator().getDataTypePropertyInfo(field));
        }
    }

    protected void initUQConstraintInfos1() {
        if(obj.getContainConstraints()==null||obj.getContainConstraints().size()==0)
            return;
        for(GspUniqueConstraint gspUniqueConstraint: obj.getContainConstraints()){
            UniqueConstraintInfo uniqueConstraintInfo=new UniqueConstraintInfo();
            uniqueConstraintInfo.setDisplayValueKey(gspUniqueConstraint.getI18nResourceInfoPrefix()+".TipInfo");
            ArrayList<String> elements = new ArrayList<>();
            for(String eleID:gspUniqueConstraint.getElementList())
            {
                elements.add(obj.findElement(eleID).getLabelID());
            }
            uniqueConstraintInfo.setFields(elements);
            uniqueConstraintInfo.setNodeCode(obj.getCode());
            addUQConstraintInfo(gspUniqueConstraint.getCode(),uniqueConstraintInfo);
        }
    }

    @Override
    public boolean isRoot() {
        return obj.getIsRootNode();
    }

    @Override
    public String getConfigId() {
        return businessEntity.getDotnetGeneratedConfigID();
    }

    protected void initChildInfos1(CefModelResInfoImpl modelResInfo) {
        if(obj.getContainChildObjects()==null||obj.getContainChildObjects().size()==0)
            return;
        for(IGspCommonObject childObj:obj.getContainChildObjects())
        {
            addChildEntityResInfo(new EntityResourceInfo((GspBizEntityObject) childObj,beModelResInfo,businessEntity));
        }
    }

}
